import {
  postAddIcon,
  getIconList,
  postUpdateIcon,
  postOnIcon,
  postOffIcon,
  getVersionBySource,
} from '../../services/channel/api';


export default {
  namespace: 'homepageIcon',

  state: {

    loading: true,

    iconConfigData: {
      list: [],
      pagination:{}
    },

    versionList: [],

  },

   effects: {

    *fetchPostAddIcon({payload,callback}, { call, put }) {
      const response = yield call(postAddIcon, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {postAddIconData: response.data},
        });
      }
      if (callback && response) callback(response);
    },

    *fetchGetIconVersion({payload,callback}, { call, put }) {
      const response = yield call(getVersionBySource, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {versionList: response.data},
        });
      }
      if (callback && response) callback(response);
    },

    *fetchGetIconList({payload,callback}, { call, put }) {
      const response = yield call(getIconList, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: { 
            iconConfigData: {
              list: response.data.records,
              pagination: {
                current: response.data.current,
                total: response.data.total,
                size: response.data.size,
              }
            }
          },
        });
      }
      if (callback && response) callback(response);
    },

    *fetchPostUpdateIcon({payload,callback}, { call, put }) {
      const response = yield call(postUpdateIcon, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {postUpdateIconRes: response.data}
        });
      }
      if (callback && response) callback(response);
    },

    *fetchPostOffIcon({payload,callback}, { call, put }) {
      const response = yield call(postOffIcon, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {postOnoffIconRes: response.data}
        });
      }
      if (callback && response) callback(response);
    },

    *fetchPostOnIcon({payload,callback}, { call, put }) {
      const response = yield call(postOnIcon, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {postOnoffIconRes: response.data}
        });
      }
      if (callback && response) callback(response);
    },

  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
